package com.huang.community.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.community.filter.SensitiveFilter;
import com.huang.community.mapper.CommentMapper;
import com.huang.community.pojo.Comment;
import com.huang.community.pojo.DiscussPost;
import com.huang.community.service.CommentService;
import com.huang.community.service.DiscussPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

import static com.huang.community.common.CommunityConstant.ENTITY_TYPE_POST;
/**
 * 评论 服务类
 * @author TianTian
 * @date 2022/4/28 22:26
 */
@Service
public class CommentServiceImp extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SensitiveFilter sensitiveFilter;
    @Autowired
    private DiscussPostService discussPostService;
    //根据实体类型查询评论
    @Override
    public List<Comment> findCommentsByEntity(Integer entityType, Integer postId, int offest, int limit) {
        Page<Comment> commentPage = new Page<>(offest, limit);
        Page<Comment> commentPages =
                this.baseMapper.selectPage(commentPage,
                        new QueryWrapper<Comment>()
                                .eq("entity_type", entityType)
                                .eq("entity_id", postId));
        List<Comment> records = commentPages.getRecords();
        return records;
    }
    //查询实体的所有评论
    @Override
    public int findCommentCount(Integer entityType, Integer entityId) {
        return this.baseMapper.selectCount(new QueryWrapper<Comment>()
                .eq("entity_type", entityType)
                .eq("entity_id", entityId));

    }
    //查询此用户的所有评论数量
    @Override
    public int findCommentCountByUserId(Integer userId) {
        return this.baseMapper.selectCount(new QueryWrapper<Comment>()
                .eq("user_id", userId)
                .eq("entity_type", 1));

    }
    //查询此用户的所有评论
    public List<Comment> findCommentOnlyComByuid(Integer userId, int offest, int limit) {
        if (userId == null) {
            throw new RuntimeException("该用户不存在!");
        }
        Page<Comment> commentPage = new Page<>(offest, limit);
        Page<Comment> commentPages =
                this.baseMapper.selectPage(commentPage, new QueryWrapper<Comment>()
                        .eq("user_id", userId)
                        .eq("entity_type", 1));
        List<Comment> records = commentPages.getRecords();
        return records;
    }
    //新增评论
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public int addComment(Comment comment) {
        if (comment == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }
        // 添加评论
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        int rows = this.baseMapper.insert(comment);
        // 更新帖子评论数量
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            int count = this.baseMapper.selectCount(new QueryWrapper<Comment>().eq("entity_id", comment.getEntityId()).eq("entity_type", comment.getEntityType()));
            discussPostService.getBaseMapper().update(null, new UpdateWrapper<DiscussPost>().eq("id", comment.getEntityId()).set("comment_count", count));
        }

        return rows;
    }
    //查找评论
    @Override
    public Comment findCommentById(int commentId) {
        return this.commentMapper.selectById(commentId);
    }
}
